package com.iven.xie.controller;

import com.iven.xie.beans.ResObject;
import com.iven.xie.entity.DbConfig;
import com.iven.xie.entity.DbTable;
import com.iven.xie.entity.DbTableColumn;
import com.iven.xie.holder.SpringContextHolder;
import com.iven.xie.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author iven.xie
 */
@Api(tags = "数据库表结构相关")
@RestController
@RequiredArgsConstructor
@RequestMapping("api/db/table")
@Slf4j
public class TableController {
    @Autowired
    private SpringContextHolder springContextHolder;
    @Autowired
    private ConfigService configService;

    @ApiOperation(value = "根据设置获取表相关信息", notes = "根据设置获取表相关信息")
    @GetMapping("/info/list")
    public ResObject<List<DbTable>> getTableInfoList(@RequestParam("connName") String connName) throws Exception {
        DbConfig dbConfig = configService.getDeFaultConfig().get(connName);
        if (Objects.isNull(dbConfig)) {
            throw new Exception("相关配置不存在");
        }
        ResObject resObject = new ResObject();
        resObject.setCode(HttpStatus.OK.value());
        resObject.setData(springContextHolder.getTableService(dbConfig.getDbType()).getTableInfoList(dbConfig));
        return resObject;
    }

    @ApiOperation(value = "根据设置获取表字段相关信息", notes = "根据设置获取表相关信息")
    @GetMapping("/table/columns")
    public ResObject<List<DbTableColumn>> getTableColumns(@RequestParam("table") String table,
                                                          @RequestParam("connName") String connName) throws Exception {
        DbConfig dbConfig = configService.getDeFaultConfig().get(connName);
        if (Objects.isNull(dbConfig)) {
            throw new Exception("相关配置不存在");
        }
        ResObject resObject = new ResObject();
        resObject.setCode(HttpStatus.OK.value());
        resObject.setData(springContextHolder.getTableService(dbConfig.getDbType()).getTableColumns(table, dbConfig));
        return resObject;
    }

    @ApiOperation(value = "根据设置获取全量表字段相关信息", notes = "根据设置获取全量表相关信息")
    @GetMapping("/mutil/table/columns")
    public ResObject<List<DbTableColumn>> getTableColumns(@RequestParam("connName") String connName) throws Exception {
        DbConfig dbConfig = configService.getDeFaultConfig().get(connName);
        if (Objects.isNull(dbConfig)) {
            throw new Exception("相关配置不存在");
        }
        ResObject resObject = new ResObject();
        resObject.setCode(HttpStatus.OK.value());
        resObject.setData(springContextHolder.getTableService(dbConfig.getDbType()).getMutilTableColumns(dbConfig));
        return resObject;
    }

}
